#include "../include/vfshash.h"
#include "../include/vfslinkedlist.h"
#include<stdio.h>
#include<malloc.h>



//Creating a node for inserting a new file
l_node *createNode()
{
	l_node *newNode = (l_node *)malloc(sizeof(l_node));
	return newNode;
}


//Inserting into linkedlist (in sorted order always)
l_node *insert(l_node *start, char f_name[])
{
	l_node *newNode, *currNode, *prevNode;

	newNode=createNode();

	//assigning filename address to key
	newNode->key=(char *) malloc(sizeof(char )*10);
	strcpy((char *)newNode->key,f_name);


	//creating a node of the type f_desc, to which data part of the LL will point
	newNode->data = (f_desc *)malloc(sizeof(f_desc));
	strcpy(((f_desc *)(newNode->data))->filename, f_name);

	//((f_desc *)(newNode->data))->filename=(char *) malloc(sizeof(char)*10);

	//point to the next node of LL
	newNode->next=NULL;
	
	if(start==NULL)
	{
	start=newNode;
	
	}
	else
	{
	//insert in sorted order
	currNode=start;
	prevNode=NULL; //This has to be NULL initially (not compulsory)
	while((currNode!=NULL) && (strcmp(f_name, (char *)currNode->key)>0))
	{
	prevNode=currNode;
	currNode=currNode->next;
	}
	if(currNode==NULL)
	{
	prevNode->next=newNode;
	}
	else
	{
	if(currNode==start) //insert at the beginning, hence need to change the start of LL
	{
	newNode->next=currNode;
	start=newNode;
	}
	
	else // insert somewhere in the middle but not in the beginning
	{
	newNode->next=currNode;
	prevNode->next=newNode;
	}
	}
	}
	return start;
}


//Displaying the contents of linked list
void display(l_node *start)
{
	l_node *temp = start;
	while(temp!=NULL)
	{
		printf("  %s", (char *)temp->key);
		temp=temp->next;
	}
	printf("\n");
}


//Creating a linkedlist out of the search results from the linked list (in case of multiple files with same name)
l_nodeSearch *insertinSearchlinkedlist(l_nodeSearch *startoflinkedlistSearch, char key[])
{
	l_nodeSearch *temp, *newNode;
	temp=startoflinkedlistSearch;

	newNode = (l_nodeSearch *)malloc(sizeof(l_nodeSearch));
	strcpy(newNode->data,key);
	newNode->next=NULL;

	if(temp==NULL)
	{
		startoflinkedlistSearch = newNode;
	}
	else
	{
		while(temp->next != NULL)
		{
			temp=temp->next;
		}
		temp->next=newNode;
	}
	return startoflinkedlistSearch;	
}


//Searching through linked list
l_nodeSearch *SearchByStringKey(l_node *start, char key[])
{
	l_nodeSearch *startoflinkedlistSearch=NULL, *newNode, *t;

	//startoflinkedlistSearch = (l_nodeSearch *)malloc(sizeof(l_nodeSearch));
	
	char nodeKey[MAX_FILE_NAME];
	l_node *temp;
	temp=start;	


	while(temp!=NULL)
	{
		if( strcmp((char *)temp->key, key)==0)
		{
			startoflinkedlistSearch=insertinSearchlinkedlist(startoflinkedlistSearch, key);			
		}	
		temp=temp->next;
		if(temp==NULL)
		{
			return startoflinkedlistSearch;
		}
	}	
	printf("\nSearch completed! No records found\n");
	return NULL;
}


//Displaying the search results
void displaySearchResults(l_nodeSearch *startofSearchlinkedlist)
{
	
	l_nodeSearch *temp;
	temp=startofSearchlinkedlist;

	while(temp!=NULL)
	{
		printf("%s  ",temp->data);
		temp = temp->next;
	}
}

//Deleting the node from the linkedlist
l_node *deleteFromLinkedlist(l_node *start, char filename[])
{
	l_node *currNode, *prevNode;
	prevNode=NULL;
	currNode=start;
	if(start!=NULL)
	{
	
		if(strcmp((char *)currNode->key, filename)==0)
		{
			start=currNode->next;
			free(currNode);
			printf("\nDeletion Successful.");
		}
		else
		{
			while(currNode!=NULL && strcmp((char *)currNode->key, filename)!=0)
			{
				prevNode=currNode;
				currNode=currNode->next;
			}
			if(currNode==NULL)
			{
				printf("\nThe filename you entered does not exist.");
			}
			else
			{
				prevNode->next=currNode->next;
				free(currNode);
				printf("\nDeletion Successful.");
			}
		}
	}
	else
	{
		printf("\n\nDeletion Unsuccessful! Hash Table is emty.");
	}
	return start;
}

